package com.vicular;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Window;
import android.view.WindowManager;
import android.widget.CompoundButton;
import android.widget.ToggleButton;

public class Setting extends Activity {

	private SharedPreferences settings;
	private ToggleButton audio;
	private ToggleButton vibrate;
	private ToggleButton scan;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.setting);
		Window window = getWindow();
		window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

		audio = (ToggleButton) findViewById(R.id.toggleAudio);
		audio.setOnCheckedChangeListener(toggleAudio);

		vibrate = (ToggleButton) findViewById(R.id.toggleVibrate);
		vibrate.setOnCheckedChangeListener(toggleVibrate);

		scan = (ToggleButton) findViewById(R.id.toggleScan);
		scan.setOnCheckedChangeListener(toggleScan);

		settings = PreferenceManager.getDefaultSharedPreferences(this);
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		audio.setChecked(settings.getBoolean(PreferencesActivity.KEY_PLAY_BEEP,
				true));
		vibrate.setChecked(settings.getBoolean(PreferencesActivity.KEY_VIBRATE,
				true));
		scan.setChecked(settings
				.getBoolean(PreferencesActivity.KEY_SCAN, false));
	}

	private final ToggleButton.OnCheckedChangeListener toggleAudio = new ToggleButton.OnCheckedChangeListener() {

		@Override
		public void onCheckedChanged(CompoundButton buttonView,
				boolean isChecked) {
			// TODO Auto-generated method stub
			SharedPreferences.Editor editor = settings.edit();
			editor.putBoolean(PreferencesActivity.KEY_PLAY_BEEP, isChecked);
			editor.commit();
		}
	};

	private final ToggleButton.OnCheckedChangeListener toggleVibrate = new ToggleButton.OnCheckedChangeListener() {

		@Override
		public void onCheckedChanged(CompoundButton buttonView,
				boolean isChecked) {
			// TODO Auto-generated method stub
			SharedPreferences.Editor editor = settings.edit();
			editor.putBoolean(PreferencesActivity.KEY_VIBRATE, isChecked);
			editor.commit();
		}
	};

	private final ToggleButton.OnCheckedChangeListener toggleScan = new ToggleButton.OnCheckedChangeListener() {

		@Override
		public void onCheckedChanged(CompoundButton buttonView,
				boolean isChecked) {
			// TODO Auto-generated method stub
			SharedPreferences.Editor editor = settings.edit();
			editor.putBoolean(PreferencesActivity.KEY_SCAN, isChecked);
			editor.commit();
		}
	};

}
